package com.yndsoft.jc.ui.memberpage.myorder;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.yndsoft.jc.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/12/6.
 */

public class OrderAdapter extends BaseAdapter {

    private Context context;
    private List<String> list = new ArrayList<>();
    private LayoutInflater inflater;

    public OrderAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<String> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        OrderViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_order_list, null);
            holder = new OrderViewHolder();
            holder.product_name = convertView.findViewById(R.id.product_name);
            holder.order_detail_btn = convertView.findViewById(R.id.order_detail_btn);
            convertView.setTag(holder);
        } else {
            holder = (OrderViewHolder) convertView.getTag();
        }

        holder.product_name.setText(list.get(position));
        holder.order_detail_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, OrderDetailActivity.class));
            }
        });
        return convertView;
    }

    class OrderViewHolder {
        private TextView product_name;
        private Button order_detail_btn;
    }
}
